package org.example.https;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslHandler;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

/**
 * @author hh
 * @since 2025/2/16
 */
public class HttpsServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 创建服务端SSL上下文实例
        SSLContext sslContext = SSLContextFactory.getSslContext();
        // 通过上下文实例创建服务端的 SSL 引擎
        SSLEngine sslEngine = sslContext.createSSLEngine();
        // 在握手的时候，使用服务端模式
        sslEngine.setUseClientMode(false);
        // /单向认证：在服务端设置不需要验证对端身份，无须客户端证实自己的身份
        sslEngine.setNeedClientAuth(false);
        // 创建SslHandler处理器，并加入到流水线
        pipeline.addLast(new SslHandler(sslEngine));
        // 请求解码器和响应编码器
        pipeline.addLast(new HttpServerCodec());
        // HttpObjectAggregator 将HTTP消息的多个部分合成一条完整的HTTP消息
        pipeline.addLast(new HttpObjectAggregator(65535));
        // 自定义的业务handler，回显HTTP请求URI、请求方法、请求参数
        pipeline.addLast(new HttpsEchoHandler());
    }
}
